package alik.notes.client;

import alik.notes.client.event.Events;
import alik.notes.client.event.Events.NoteAddEvent;
import alik.notes.client.event.Events.NotesChangeEvent;
import alik.notes.client.event.impl.NoteAddHandlerImpl;
import alik.notes.client.event.impl.NotesChangeHandlerImpl;
import alik.notes.client.gin.AppGinInjectorFactory;
import alik.notes.shared.data.Note;
import alik.notes.shared.data.NoteBean;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class NotesEntryPoint implements EntryPoint {
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootPanel contentContainer = RootPanel.get(ElementId.contentContainer.name());
		NotesTable notesTable = new NotesTable();
		contentContainer.add(notesTable);

		final Label titleLabel = new Label("Title:");
		titleLabel.setSize("100%", "100%");
		RootPanel.get(ElementId.titleLabelDiv.name()).add(titleLabel);

		final TextBox noteTitle = new TextBox();
		noteTitle.setSize("100%", "100%");
		RootPanel.get(ElementId.noteTitleTextBoxDiv.name()).add(noteTitle);

		noteTitle.setFocus(true);

		final TextArea noteDescription = new TextArea();
		noteDescription.setSize("100%", "100%");
		RootPanel.get(ElementId.noteDescTextAreaDiv.name()).add(noteDescription);
		

		final Label descLabel = new Label("Description:");
		descLabel.setSize("100%", "100%");
		RootPanel.get(ElementId.descLabelDiv.name()).add(descLabel);


		final Button addNoteButton = new Button("Add Note");
		addNoteButton.setStyleName(Style.addNoteButton.name());
		RootPanel actionContainer = RootPanel.get(ElementId.actionContainer.name());
		actionContainer.add(addNoteButton);

		addNoteButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Note note = new NoteBean(noteTitle.getText(), noteDescription.getText(), System.currentTimeMillis());
				AppGinInjectorFactory.GIN_INJECTOR.getEventBus().fireEvent(new Events.NoteAddEvent(note));
			}
		});

		AppGinInjectorFactory.GIN_INJECTOR.getEventBus().addHandler(NoteAddEvent.getType(), new NoteAddHandlerImpl());
		AppGinInjectorFactory.GIN_INJECTOR.getEventBus().addHandler(NotesChangeEvent.getType(), new NotesChangeHandlerImpl(notesTable));

		AppGinInjectorFactory.GIN_INJECTOR.getEventBus().fireEvent(new NotesChangeEvent());
	}
}